package chapter_9;

public class Ticket implements Runnable{

    int ticket = 100;

    Object object = new Object();//锁对象

    public synchronized static void test(){

    }

    @Override
    public void run() {
        while(true){
            if(ticketMinus()){
                break;
            };
        }
    }

    private synchronized boolean ticketMinus() {
        if(ticket > 0){
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "正在卖票，票号为：" + ticket--);
        }else{
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(ticket,"窗口1").start();
        new Thread(ticket,"窗口2").start();
        new Thread(ticket,"窗口3").start();
    }
}
